﻿#region Copyright (c) 2011, thinktecture (http://www.thinktecture.com)

/*
   thinktecture.UIAnnotations was developed by Jörg Neumann.
   Copyright (c) 2011, thinktecture (http://www.thinktecture.com).
   All rights reserved, comes as-is and without any warranty. Use of this
   source file is governed by the license which is contained in LICENSE.TXT 
   in the distribution.
*/

#endregion

using System;
using System.Windows;

namespace Thinktecture.UIAnnotations
{
    internal static class ElementHelpers
    {
        public static T GetParent<T>(DependencyObject element) where T : DependencyObject
        {
            if (element != null)
            {
                if (typeof(T).IsAssignableFrom(element.GetType()))
                {
                    return (T)element;
                }
                else
                {
                    DependencyObject parent = LogicalTreeHelper.GetParent(element);
                    DependencyObject result = GetParent<T>(parent);
                    if (result != null)
                        return (T)result;
                }
            }
            return default(T);
        }
    }
}
